"""initial migration

Revision ID: a3f12a7b19e0
Revises: 
Create Date: 2025-04-06 00:24:19.395652

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision: str = 'a3f12a7b19e0'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('funcall_log',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False, comment='主键'),
    sa.Column('description', sa.VARCHAR(length=512), server_default=sa.text("''"), nullable=True, comment='描述'),
    sa.Column('req_args', sa.TEXT(), nullable=True, comment='请求参数'),
    sa.Column('log_text', sa.TEXT(length=16777216), nullable=True, comment='日志信息'),
    sa.Column('remark', sa.VARCHAR(length=512), server_default=sa.text("''"), nullable=False, comment='备注'),
    sa.Column('type', sa.VARCHAR(length=16), server_default=sa.text("'tool'"), nullable=False, comment='类型'),
    sa.Column('status', sa.VARCHAR(length=8), server_default=sa.text("'0'"), nullable=False, comment='状态[0:正常,1:错误,2:执行]'),
    sa.Column('completion_tokens', sa.Integer(), server_default=sa.text("'0'"), nullable=False, comment='完成令牌'),
    sa.Column('prompt_tokens', sa.Integer(), server_default=sa.text("'0'"), nullable=False, comment='提示令牌'),
    sa.Column('total_tokens', sa.Integer(), server_default=sa.text("'0'"), nullable=False, comment='总令牌'),
    sa.Column('created_at', sa.TIMESTAMP(), nullable=False),
    sa.Column('updated_at', sa.TIMESTAMP(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    comment='接口funcall调用日志'
    )
    op.create_table('openai_log',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('request_body', mysql.LONGTEXT(), nullable=False),
    sa.Column('response_body', mysql.LONGTEXT(), nullable=True),
    sa.Column('url', sa.String(length=360), server_default=sa.text("''"), nullable=False),
    sa.Column('chat_id', sa.Integer(), nullable=True),
    sa.Column('created_at', sa.TIMESTAMP(), nullable=False),
    sa.Column('updated_at', sa.TIMESTAMP(), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_openai_log_id'), 'openai_log', ['id'], unique=False)
    op.create_table('tools',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('tool_name', sa.String(length=64), server_default=sa.text("''"), nullable=False),
    sa.Column('tool_desc', sa.String(length=255), server_default=sa.text("''"), nullable=False),
    sa.Column('remark', sa.String(length=255), server_default=sa.text("''"), nullable=False),
    sa.Column('created_at', sa.TIMESTAMP(), nullable=False),
    sa.Column('updated_at', sa.TIMESTAMP(), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_tools_id'), 'tools', ['id'], unique=False)
    op.create_table('users',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('email', sa.String(length=255), nullable=False),
    sa.Column('username', sa.String(length=255), nullable=False),
    sa.Column('nickname', sa.String(length=255), nullable=False),
    sa.Column('hashed_password', sa.String(length=255), nullable=False),
    sa.Column('is_active', sa.Boolean(), nullable=True),
    sa.Column('is_superuser', sa.Boolean(), nullable=True),
    sa.Column('created_at', sa.TIMESTAMP(), nullable=False),
    sa.Column('updated_at', sa.TIMESTAMP(), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
    op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False)
    op.create_index(op.f('ix_users_nickname'), 'users', ['nickname'], unique=True)
    op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True)
    op.create_table('ai_api',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=255), nullable=False, comment='名称[根据该名称关联]'),
    sa.Column('description', sa.String(length=255), nullable=False, comment='描述'),
    sa.Column('remark', sa.String(length=255), nullable=True, comment='备注'),
    sa.Column('api_base', sa.String(length=255), nullable=True, comment='接口地址'),
    sa.Column('api_key', sa.String(length=255), nullable=True, comment='接口密钥'),
    sa.Column('api_token', sa.String(length=255), nullable=True, comment='接口令牌'),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=True),
    sa.Column('updated_at', sa.DateTime(), nullable=True),
    sa.Column('status', sa.Integer(), nullable=True, comment='状态[1:草稿,0:完成]'),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_ai_api_id'), 'ai_api', ['id'], unique=False)
    op.create_table('api_keys',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('url', sa.String(length=255), server_default=sa.text("''"), nullable=False),
    sa.Column('public_key', sa.VARCHAR(length=128), nullable=False),
    sa.Column('secret_key', sa.VARCHAR(length=128), nullable=False),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('is_active', sa.Boolean(), nullable=False),
    sa.Column('last_used_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('created_at', sa.TIMESTAMP(), nullable=False),
    sa.Column('updated_at', sa.TIMESTAMP(), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_api_keys_id'), 'api_keys', ['id'], unique=False)
    op.create_index(op.f('ix_api_keys_public_key'), 'api_keys', ['public_key'], unique=True)
    op.create_index(op.f('ix_api_keys_secret_key'), 'api_keys', ['secret_key'], unique=True)
    op.create_table('chats',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('title', sa.String(length=255), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('created_at', sa.TIMESTAMP(), nullable=False),
    sa.Column('updated_at', sa.TIMESTAMP(), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_chats_id'), 'chats', ['id'], unique=False)
    op.create_table('knowledge_bases',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.Column('description', mysql.LONGTEXT(), nullable=True),
    sa.Column('dimensions', sa.Integer(), nullable=True, comment='向量'),
    sa.Column('enable_sparse', sa.Integer(), nullable=False, comment='启用稀疏[1:启用,0:停用]'),
    sa.Column('enable_sparse_model', sa.VARCHAR(length=64), nullable=True, comment='稀疏模型'),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=True),
    sa.Column('updated_at', sa.DateTime(), nullable=True),
    sa.Column('status', sa.String(length=32), server_default='draft', nullable=True, comment='状态[draft:草稿,closed:已关闭]'),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_knowledge_bases_id'), 'knowledge_bases', ['id'], unique=False)
    op.create_table('ai_agents',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.Column('description', sa.String(length=255), server_default=sa.text("''"), nullable=False),
    sa.Column('emoji', sa.String(length=16), server_default=sa.text("''"), nullable=False),
    sa.Column('group', sa.String(length=128), server_default=sa.text("''"), nullable=False),
    sa.Column('system_prompt', sa.TEXT(length=16777216), nullable=True),
    sa.Column('summarize_prompt', sa.String(length=2048), server_default=sa.text("''"), nullable=False, comment='总结提示词'),
    sa.Column('tools', sa.String(length=2048), server_default=sa.text("''"), nullable=False, comment='工具列表'),
    sa.Column('kb_id', sa.Integer(), nullable=True),
    sa.Column('agent_ids', sa.String(length=2048), server_default=sa.text("''"), nullable=False, comment='关联的AI助手ID'),
    sa.Column('sync_flag', sa.Integer(), server_default=sa.text('1'), nullable=False, comment='同步标识'),
    sa.Column('created_at', sa.TIMESTAMP(), nullable=False),
    sa.Column('updated_at', sa.TIMESTAMP(), nullable=False),
    sa.ForeignKeyConstraint(['kb_id'], ['knowledge_bases.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_ai_agents_id'), 'ai_agents', ['id'], unique=False)
    op.create_table('chat_knowledge_bases',
    sa.Column('chat_id', sa.Integer(), nullable=False),
    sa.Column('knowledge_base_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['chat_id'], ['chats.id'], ),
    sa.ForeignKeyConstraint(['knowledge_base_id'], ['knowledge_bases.id'], ),
    sa.PrimaryKeyConstraint('chat_id', 'knowledge_base_id')
    )
    op.create_table('documents',
    sa.Column('id', sa.Integer(), nullable=False, comment='ID'),
    sa.Column('sub_path', sa.String(length=255), nullable=False, comment='存档目录'),
    sa.Column('description', sa.String(length=255), nullable=False, comment='描述'),
    sa.Column('file_name', sa.String(length=255), nullable=False, comment='文件名'),
    sa.Column('file_size', sa.BigInteger(), nullable=False, comment='文件大小'),
    sa.Column('content_type', sa.String(length=100), nullable=False, comment='文件类型'),
    sa.Column('file_hash', sa.String(length=64), nullable=True, comment='SHA-256哈希值'),
    sa.Column('knowledge_base_id', sa.Integer(), nullable=False, comment='知识库ID'),
    sa.Column('status', sa.String(length=32), server_default='draft', nullable=False, comment='状态'),
    sa.Column('status_time', sa.DateTime(), nullable=True, comment='状态时间'),
    sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='创建时间'),
    sa.Column('updated_at', sa.DateTime(), nullable=True, comment='更新时间'),
    sa.Column('error_message', sa.Text(), nullable=True, comment='错误信息'),
    sa.ForeignKeyConstraint(['knowledge_base_id'], ['knowledge_bases.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('knowledge_base_id', 'sub_path', name='uq_kb_file_name')
    )
    op.create_index(op.f('ix_documents_file_hash'), 'documents', ['file_hash'], unique=False)
    op.create_index(op.f('ix_documents_id'), 'documents', ['id'], unique=False)
    op.create_table('messages',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('content', mysql.LONGTEXT(), nullable=False),
    sa.Column('role', sa.String(length=50), nullable=False),
    sa.Column('chat_id', sa.Integer(), nullable=False),
    sa.Column('problem_id', sa.Integer(), nullable=True, comment='问题ID'),
    sa.Column('created_at', sa.TIMESTAMP(), nullable=False),
    sa.Column('updated_at', sa.TIMESTAMP(), nullable=False),
    sa.ForeignKeyConstraint(['chat_id'], ['chats.id'], ),
    sa.ForeignKeyConstraint(['problem_id'], ['messages.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_messages_id'), 'messages', ['id'], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_messages_id'), table_name='messages')
    op.drop_table('messages')
    op.drop_index(op.f('ix_documents_id'), table_name='documents')
    op.drop_index(op.f('ix_documents_file_hash'), table_name='documents')
    op.drop_table('documents')
    op.drop_table('chat_knowledge_bases')
    op.drop_index(op.f('ix_ai_agents_id'), table_name='ai_agents')
    op.drop_table('ai_agents')
    op.drop_index(op.f('ix_knowledge_bases_id'), table_name='knowledge_bases')
    op.drop_table('knowledge_bases')
    op.drop_index(op.f('ix_chats_id'), table_name='chats')
    op.drop_table('chats')
    op.drop_index(op.f('ix_api_keys_secret_key'), table_name='api_keys')
    op.drop_index(op.f('ix_api_keys_public_key'), table_name='api_keys')
    op.drop_index(op.f('ix_api_keys_id'), table_name='api_keys')
    op.drop_table('api_keys')
    op.drop_index(op.f('ix_ai_api_id'), table_name='ai_api')
    op.drop_table('ai_api')
    op.drop_index(op.f('ix_users_username'), table_name='users')
    op.drop_index(op.f('ix_users_nickname'), table_name='users')
    op.drop_index(op.f('ix_users_id'), table_name='users')
    op.drop_index(op.f('ix_users_email'), table_name='users')
    op.drop_table('users')
    op.drop_index(op.f('ix_tools_id'), table_name='tools')
    op.drop_table('tools')
    op.drop_index(op.f('ix_openai_log_id'), table_name='openai_log')
    op.drop_table('openai_log')
    op.drop_table('funcall_log')
    # ### end Alembic commands ###
